package com.zzsy.carosshopprojects.activitys.home.mainpage.menu;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.login.LoginAc;
import com.zzsy.carosshopprojects.adapter.MenuAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.util.SPUtil;
import com.zzsy.carosshopprojects.view.dialog.ComTwoBtnDialog;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主页侧滑菜单
 */
public class HomeMenuFragment extends BaseFragment implements MenuAdapter.OnSwithChageListener {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.tvChangeShift)
    TextView tvChangeShift;
    @BindView(R.id.tvSafeQuit)
    TextView tvSafeQuit;
    private MenuAdapter menuAdapter;
    private HomeMenuMode homeMenuMode;
    @BindDimen(R.dimen.p_180)
    int p_180;//获取尺寸大小
    private String type = "";

    @Override
    protected int initLayout() {
        return R.layout.fg_homemenu;
    }

    @Override
    protected void initView(View view) {
//        tvChangeShift.setBackground(ViewBgUtil.setBg(p_180, Color.parseColor(ConfigureFile.ColorSystem)));
//        tvSafeQuit.setBackground(ViewBgUtil.setBg(p_180, Color.parseColor(ConfigureFile.ColorSystem)));
    }

    @Override
    protected void initData(Context mContext) {
        menuAdapter = new MenuAdapter(getContext(), null, 0);
        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleView.setAdapter(menuAdapter);
        menuAdapter.setListener(this);
        homeMenuMode = getViewModelProvider().get(HomeMenuMode.class);
    }

    @Override
    protected void loadMethod() {
        showListData();
        setCheckState();
    }

    @OnClick({R.id.tvChangeShift, R.id.tvSafeQuit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvChangeShift://交接班
                changeShift();
                break;
            case R.id.tvSafeQuit://安全退出
                safeQuit();
                break;
        }
    }

    @Override
    public void onSwitch(boolean isSwitch, String type) {
        this.type = type;
        if (type.equals("1"))//是否开具发票
        {
            homeMenuMode.updateQrSwitch(getContext(), isSwitch == true ? "1" : "0");
        } else if (type.equals("2")) //是否语音播报
        {
            SPUtil.getInstance().putString(ConfigureFile.MUSICSWITCH, isSwitch == true ? "1" : "0");
            menuAdapter.setCheckState(isSwitch == true ? "1" : "0", type);
        } else if (type.equals("3")) //是否打印公众号小票
        {
            SPUtil.getInstance().putString(ConfigureFile.GZHSWITCH, isSwitch == true ? "1" : "0");
            menuAdapter.setCheckState(isSwitch == true ? "1" : "0", type);
        } else if (type.equals("4")) //是否打印交接班小票
        {
            SPUtil.getInstance().putString(ConfigureFile.CHANGESHIFESTICKET, isSwitch == true ? "1" : "0");
            menuAdapter.setCheckState(isSwitch == true ? "1" : "0", type);
        } else if (type.equals("5")) //是否打印消费及充值小票
        {
            SPUtil.getInstance().putString(ConfigureFile.OTHERTICKET, isSwitch == true ? "1" : "0");
            menuAdapter.setCheckState(isSwitch == true ? "1" : "0", type);
        }
    }

    /**
     * 显示menu数据
     */
    private void showListData() {
        homeMenuMode.getLiveData().observe(this, MenuBeans -> {
            menuAdapter.setListData(MenuBeans);
            menuAdapter.notifyDataSetChanged();
        });
        homeMenuMode.postHomeMenuListBean(getContext());
    }

    /**
     * 设置是否开具发票开关显示状态
     */
    private void setCheckState() {
        homeMenuMode.getLiveDataQrSwitch().observe(this, resultBean -> {//操作不成功返回CheckBox之前的状态
            menuAdapter.setCheckState(resultBean.getState(), type);
        });
    }

    /**
     * 交接班
     */
    private void changeShift() {
        ComTwoBtnDialog handoverDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.DIALOG_DO_HANDOVER, null);
        handoverDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                handoverDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                handoverDialog.dismiss();
                homeMenuMode.DoHandoverWho(getContext());
            }
        });
        handoverDialog.show();
    }

    /**
     * 安全退出
     */
    private void safeQuit() {
        ComTwoBtnDialog exitLoginDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.DIALOG_CLEAR_CACHE, null);
        exitLoginDialog.show();
        exitLoginDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                exitLoginDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                loginOut();
            }
        });
    }

    /**
     * 退出
     */
    private void loginOut() {
        SPUtil.getInstance().putBoolean(ConfigureFile.ISLOGIN, false);
        Intent intent = new Intent(getActivity(), LoginAc.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
